package com.mdd.service.pay.impl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.*;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import com.mdd.service.pay.AlipayAuthTradeService;
import com.mdd.service.pay.AlipayTradeOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 * {@code @Description}
 * @Author 幻速
 * {@code @Date} 2023/10/30 17:42
 **/
@Slf4j
@Service
public class AlipayTradeOrderServiceImpl implements AlipayTradeOrderService {
    /**
     * 支付接口（代扣/预授权扣款）
     * @param alipayClient 请求对象
     * @param alipayTradeCreateModel 支付参数（代扣/预授权扣款）
     * @return 返回支付结果
     * @throws AlipayApiException 支付异常
     */
    @Override
    public AlipayTradeCreateResponse tradePay(AlipayClient alipayClient, AlipayTradeCreateModel alipayTradeCreateModel, String notifyUrl) throws AlipayApiException {
        log.info("AlipayTradePayRequest   params----->"+alipayTradeCreateModel);
        AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
        request.setBizModel(alipayTradeCreateModel);
        request.setNotifyUrl(notifyUrl);
        AlipayTradeCreateResponse response = alipayClient.certificateExecute(request);
        log.info("AlipayTradePayRequest-------->"+JSON.toJSONString(response));
        return response;
    }

    /**
     * 收单交易关闭
     * @param alipayClient 请求对象
     * @param alipayTradeRefundModel 关闭预授权参数
     * @param notifyUrl 收单交易通知
     */
    @Override
    public AlipayTradeRefundResponse refund(AlipayClient alipayClient, AlipayTradeRefundModel alipayTradeRefundModel) throws AlipayApiException {
        log.info("alipayTradeRefundRequest  params------->"+JSON.toJSONString(alipayTradeRefundModel));
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        request.setBizModel(alipayTradeRefundModel);
        AlipayTradeRefundResponse response = alipayClient.certificateExecute(request);
        log.info("AlipayTradeRefundResponse------->"+JSON.toJSONString(response));
        return response;
    }

    /**
     * 交易查询
     * @param alipayClient 请求对象
     * @param tradeQueryModel 交易查询
     * @param notifyUrl 交易通知
     * @return 查询交易结果
     * @throws AlipayApiException 查询异常
     */
    @Override
    public AlipayTradeQueryResponse tradeQuery(AlipayClient alipayClient, AlipayTradeQueryModel tradeQueryModel, String notifyUrl) throws AlipayApiException {
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        log.info("AlipayTradeQueryRequest   params------>"+JSON.toJSONString(tradeQueryModel));
        request.setNotifyUrl(notifyUrl);
        request.setBizModel(tradeQueryModel);
        AlipayTradeQueryResponse response = alipayClient.certificateExecute(request);
        log.info("AlipayTradeQueryResponse----->"+JSON.toJSONString(response));
        return response;
    }

    /**
     * 手机网站支付
     * @param alipayClient
     * @param alipayTradeWapPayModel
     * @param notifyUrl
     * @return
     * @throws AlipayApiException
     */
    @Override
    public AlipayTradeWapPayResponse tradePay(AlipayClient alipayClient, AlipayTradeWapPayModel alipayTradeWapPayModel, String notifyUrl,String returnUrl) throws AlipayApiException {
        log.info("alipayTradeWapPayModel   params----->"+alipayTradeWapPayModel);
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        request.setBizModel(alipayTradeWapPayModel);
        request.setNotifyUrl(notifyUrl);
        request.setReturnUrl(returnUrl);
        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request);
        log.info("AlipayTradePayRequest-------->"+JSON.toJSONString(response));
        return response;
    }

    /**
     * 电脑网站
     * @param alipayClient
     * @param alipayTradePagePayModel
     * @param notifyUrl
     * @return
     * @throws AlipayApiException
     */
    @Override
    public AlipayTradePagePayResponse tradePay(AlipayClient alipayClient, AlipayTradePagePayModel  alipayTradePagePayModel, String notifyUrl) throws AlipayApiException {
        log.info("alipayTradePagePayModel   params----->"+alipayTradePagePayModel);
        AlipayTradePagePayRequest  request = new AlipayTradePagePayRequest ();
        request.setBizModel(alipayTradePagePayModel);
        request.setNotifyUrl(notifyUrl);
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
        log.info("AlipayTradePayRequest-------->"+JSON.toJSONString(response));
        return response;
    }

}
